<template>
    <div class="ProcessToolBar">
        <rx-button-list :buttonLoading="buttonLoading" :localButtons="getButtons" @change="handMethod"></rx-button-list>
    </div>
</template>

<script>
import startButton from './js/startButton';
import formBase from './js/formBase.js';
import BpmInstApi from "../../../api/rxBpm/bpmInst";
import RxPicker from '@/components/RxPicker';
import RxButtonList from "@/components/RxButtonList"
import Dialog from "@/js/Dialog";

export default {
    name: "ProcessToolBar",
    mixins: [startButton, formBase],
    components: {
        RxPicker,
        RxButtonList
    },
    props: {
        processConfig: {type: Object},
        defId: {type: String},
        instId: {type: String},
        mainDefId: {type: String},
        mainTaskId: {type: String},
        vars: {
            type: Object, default: () => {
            }
        },
        appId: String,
    },
    data() {
        return {
            saveDraftStatus: false,
            startProcessStatus: false,
            buttons: [],
            startConfirm: false,
            spinShow: false,
            submited: false,
            saveed: false,
            //是否需要弹出确认的对话框
            needConfirmDlg: false,
            moreShow: false,
            buttonLoading: {
                key: '',
                loading: false
            },//按钮Loading
        }
    },
    methods: {
        ToolBarI18n(name,text,key){
            return this.$ti18(name,text,"bpmTaskLang.ProcessToolBar",key);
        },
        /*
        保存草稿
         */
        async saveDraft() {
            let data = this.getData()
            let varData = {mainDefId: this.mainDefId, mainTaskId: this.mainTaskId}
            if (this.vars) {
                varData = Object.assign(varData, this.vars)
            }
            data.vars = JSON.stringify(varData);
            BpmInstApi.saveDraft(data).then((res) => {
                this.buttonLoading = {
                    key:'',
                    loading:false,
                }
                if (res.success) {
                    uni.showToast({
                        title: res.message, // 消息内容
                        duration: 2000, // 持续时间（单位：毫秒）
                        icon: 'none' // 图标类型（可选值：success/loading/none）
                    });
                } else {
                    uni.showToast({
                        title: res.message, // 消息内容
                        duration: 2000, // 持续时间（单位：毫秒）
                        icon: 'none' // 图标类型（可选值：success/loading/none）
                    });
                }
                this.saveDraftStatus = false
                this.saveed = true
                this.rxForms._afterSubmit(res, data, 'saveDraft');
                this.clickNavLeft()
            })
        },
        /*
        启动流程
         */
        async startProcess() {
            if (this.submited) {
                this.buttonLoading = {
                    key:'',
                    loading:false,
                }
                uni.showToast({
                    title:this.ToolBarI18n('repeatTips','请勿重复提交流程'), // 消息内容
                    duration: 2000, // 持续时间（单位：毫秒）
                    icon: 'none' // 图标类型（可选值：success/loading/none）
                });
                return
            }
            let parity = await this.rxForms.formCheckRule();
            if (!parity.success) {
                //校验不通过
                uni.showToast({
                    title: parity.msg, duration: 2000, icon: 'none'
                });
                this.buttonLoading = {
                    key:'',
                    loading:false,
                }
                return
            }
            //在提交之前执行进行验证返回验证结果信息;
            this.rxForms._beforeSubmit(parity);
            let self_ = this
            if (self_.startConfirm && !self_.needConfirmDlg) {
                //启动流程确认
                uni.showModal({
                    title:this.ToolBarI18n('tipInfo','提示信息'),
                    content:this.ToolBarI18n('confirmStartFlow','确认启动流程吗？'),
                    success: function (res) {
                        if (res.confirm) {
                            self_.startFlow()
                        } else if (res.cancel) {
                            self_.buttonLoading = {
                                key:'',
                                loading:false,
                            }

                        }
                    }
                });
            } else if (self_.needConfirmDlg) {
                this.buttonLoading = {
                    key:'',
                    loading:false,
                }
                let formData = this.rxForms.getData();
                let dataObj = {
                    formSolutionAlias: this.rxForms.resForm.alias,
                    processConfig: self_.processConfig,
                    defId: self_.defId,
                    formData: formData,
                    instId: self_.instId || formData.instId || formData.INST_ID_
                }
                let url = '/views/modules/bpm/task/BpmInstStartConfirm?appId=' + this.appId;
                uni.setStorageSync('BpmInstStartConfirm', JSON.stringify(dataObj))
                uni.navigateTo({url: url})
            } else {
                self_.startFlow()
            }
        },
        /*
        提交启动流程
         */
        async startFlow() {
            let self = this
            self.spinShow = true
            this.startProcessStatus = true
            let data = this.getData()

            var varData = {mainDefId: this.mainDefId, mainTaskId: this.mainTaskId}
            if (this.vars) {
                varData = Object.assign(varData, this.vars)
            }
            data.vars = JSON.stringify(varData)
            BpmInstApi.startProcess(data).then((res) => {
                self.buttonLoading = {
                    key:'',
                    loading:false,
                }
                self.rxForms._afterSubmit(res, data)
                self.startProcessStatus = false
                self.spinShow = false
                if (res.success) {
                    uni.showToast({title: res.message, duration: 2000, icon: 'none'});
                    this.clickNavLeft()
                } else {
                    uni.showToast({title: res.message, duration: 2000, icon: 'none'});
                }
            })
                .catch((error) => {
                    self.startProcessStatus = false
                    self.spinShow = false
                })
        },
        /*
        流程图
         */
        flowChart(bth) {
            let self_ = this;
            let formData = this.rxForms.getData();
            self_.buttonLoading = {
                key:'',
                loading:false,
            }
            let conf = {
                data: {
                    defId: self_.defId,
                    formData: formData,
                    dialogOptions: {
                        title:this.ToolBarI18n('btnFlowImg','流程图'),
                        okText: ''
                    },
                },
                curVm: this,
            };
            Dialog.openBpmImageView(conf, function (obj) {
            })
        },
    },
    watch: {
        processConfig: {
            handler(newVal, oldVal) {
                if (!newVal) {
                    return
                }
                this.buttons = newVal.buttonConfigs

                let fillOpinion = false
                let assignFlowUsers = false
                let startCalFlowusers = false
                let relInsts = false
                //允许选择路径
                let allowSelectPath = newVal.allowSelectPath
                let assignBatchFlowUsers = newVal.assignBatchFlowUsers
                var startOptions = newVal.startNodeOptions

                if (startOptions instanceof Array) {
                    this.startConfirm = startOptions.indexOf('startConfirm') != -1
                    fillOpinion = startOptions.indexOf('fillOpinion') != -1
                    assignFlowUsers = startOptions.indexOf('assignFlowUsers') != -1
                    startCalFlowusers = startOptions.indexOf('startCalFlowusers') != -1
                    relInsts = startOptions.indexOf('relInsts') != -1
                }
                if (
                    assignBatchFlowUsers ||
                    fillOpinion ||
                    assignFlowUsers ||
                    startCalFlowusers ||
                    allowSelectPath ||
                    relInsts
                ) {
                    //需要显示弹出对话框
                    this.needConfirmDlg = true
                }
            },
            deep:true,
            immediate:true
        },
    },
}
</script>

<style scoped>
.ProcessToolBar {
    width: 100%;
    display: flex;
}
</style>
